<?php

namespace App\Models\Traits;

use Carbon\Carbon;
use App\Models\Address;

trait OrderTrait
{
	public static function isAddressSupported(Address $address): bool
	{
		return !in_array($address->province, self::$noAreas);
	}


	/*==================================================================================================================*/


	public function getStatus(): string
	{
		if (!$this->isPaid() and !$this->isClosed()) {
			return '待付款';
		} elseif ($this->isPaid() and !$this->isCancelled() and ($this->delivery_status === 'pending')) {
			return '待发货';
		} elseif ($this->isPaid() and $this->isCancelled()) {
			return '已取消';
		} elseif ($this->isPaid() and ($this->delivery_status === 'delivered')) {
			return '已发货';
		} elseif ($this->isPaid() and ($this->delivery_status === 'received')) {
			return '已收货';
		} elseif ($this->isPaid() and ($this->delivery_status === 'received') and !$this->has_commented) {
			return '待评价';
		}
		return '';
	}

	public function getAftersaleStatus()
	{
		if (! $refund = $this->refunds()->first()) return '';
		return self::$refundStatus[$refund->refund_status];
	}

	public function isAftersaleAvailable(): bool
	{
		/*if ($this->isGoldBeanOrder()) return false;
		if ($this->isGoldStormOrder()) return false;*/
		if (!$this->isProductReturnable()) return false;

		return $this->isPaid() and !$this->has_refund
			and $this->created_at->gt(Carbon::now()->addDays(-15));
	}
}
